package model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

public class teacher{

    private String Taccount;

    private int Cno;

    private HashMap<String,String> nature;




    public HashMap<String,String> getNature() {
        return nature;
    }

    public void setNature(HashMap<String,String> nature) {
        this.nature = nature;
    }

    public teacher(String taccount){
        Taccount = taccount;
    }

    public String getTaccount() {
        return Taccount;
    }

    public void setTaccount(String taccount) {
        Taccount = taccount;
    }

    public int getCno() {
        return Cno;
    }

    public void setCno(int cno) {
        Cno = cno;
    }

    public static HashMap<Integer,classes> seeClass(String account){

        Connection con=jdbcUtil.jdbcUtil.getConnection();

        PreparedStatement pstm=null;

        ResultSet rs=null;

        HashMap<Integer,classes> hashMap=new HashMap<>();

        try {
            pstm=con.prepareStatement("select c.* from classes c where c.`Cno` in (select t.Cno from teacher t where t.`Taccount`=?)");

            pstm.setString(1,account);

            rs=pstm.executeQuery();

            while(rs.next()){

                int Cno=rs.getInt("Cno");

                String Cname=rs.getString("Cname");

                if(Cname==null){
                    Cname="暂无数据";
                }

                classes cl=new classes(Cno,Cname);

                hashMap.put(Cno,cl);

            }
        } catch (SQLException e) {
            e.printStackTrace();
            e.printStackTrace();
            System.out.println("失败-->teacher.java-->seeClass()");
        }finally {
            jdbcUtil.jdbcUtil.close(null,pstm,rs);
        }

        return hashMap;

    }

    public static boolean addTeacherInfo(String taccount,int cno){
        Connection con=jdbcUtil.jdbcUtil.getConnection();

        PreparedStatement pstm=null;

        ResultSet rs=null;

        int num=0;

        try {
            pstm=con.prepareStatement("INSERT INTO teacher (Taccount,Cno) VALUES (?,?)");

            pstm.setString(1,taccount);
            pstm.setInt(2,cno);


            num=pstm.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("失败--->teacher.java-->addTeacherInfo()");
            return false;
        }finally {
            jdbcUtil.jdbcUtil.close(null,pstm,rs);
        }

        if(num!=0){
            return true;
        }else{
            return false;
        }
    }

}
